<?php
include_once('settings.php');
header('content-type: application/json; charset=utf-8');
if(array_key_exists('json',$_GET)){
	// injection vedelem
	$blocker = '';
	$blocked = '';
	if(array_key_exists('blocker',$_GET))
		$blocker = addslashes($_GET['blocker']);
	if(array_key_exists('blocked',$_GET))
		$blocked = addslashes($_GET['blocked']);
	// uj block hozzaadasa
	if(array_key_exists('add',$_GET)){
		insert_block($blocker,$blocked);
	}
	// meglevo block torlese
	else if(array_key_exists('remove',$_GET)){
		remove_block($blocker,$blocked);
	}
	// userhez tartozo aktualis lista kiirasa
	$data = list_blocked($blocker);
	echo $_GET['callback'] . '('.json_encode($data).')';
}
function list_blocked($blocker){
	// lista osszeallitas
	$q = mysql_query("SELECT blocked FROM block WHERE blocker=$blocker");
	$names = array();
	if($q)
		if(mysql_num_rows($q) > 0)
			while($s=mysql_fetch_array($q)){
				$names[]=$s["blocked"];
			};
	return $names;
}
function insert_block($blocker,$blocked){
	// ne legyen duplikacio
	if(!block_exists($blocker,$blocked)){		
		$q = mysql_query("INSERT INTO block (blocker,blocked) VALUES($blocker,$blocked)");
	}
}
function remove_block($blocker,$blocked){
	// ellenorzes
	if(block_exists($blocker,$blocked)){
		$q = mysql_query("DELETE FROM block WHERE blocker=$blocker AND blocked=$blocked");
	}
}
function block_exists($blocker,$blocked){
	// kereses
	$q = mysql_query("SELECT * FROM block WHERE blocker=$blocker AND blocked=$blocked");
	// ha mar van, akkor true
	if($q)
		if(mysql_num_rows($q) > 0)
			return true;
	// egyebkent false
	return false;
}
?>